package canglang.monitor.reports.action;

import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.codelookup.service.CodeLookupMgr;
import canglang.monitor.reports.service.ReportsMgr;
import canglang.monitor.reports.service.impl.ReportsMgrImpl;
import canglang.monitor.reports.to.SearchTO;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class ReportsAction extends BaseAction {
    private ReportsMgr reportsMgr;
    private CodeLookupMgr codeLookupMgr;
    private SearchTO searchTO;

    public SearchTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchTO searchTO) {
        this.searchTO = searchTO;
    }

    public CodeLookupMgr getCodeLookupMgr() {
        return codeLookupMgr;
    }

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public String generateReport() {
        if (searchTO == null) {
            searchTO = new canglang.monitor.reports.to.SearchTO();
            return NONE;
        }

        HashMap map = setHashMapByTO(searchTO);
        getResponse().reset();
        getResponse().setContentType("application/vnd.ms-excel");
        try {
            byte[] buf = reportsMgr.genereateReport(searchTO.getStartDate(), searchTO.getEndDate(), searchTO.getDeptId(), map);
            getResponse().setContentLength(buf.length);
            getResponse().getOutputStream().write(buf, 0, buf.length);
            getResponse().getOutputStream().flush();
            getResponse().getOutputStream().close();

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return NONE;
    }


    public String generateImgeReport() {
        return SUCCESS;
    }

    private HashMap setHashMapByTO(SearchTO searchTO) {
        HashMap map = new HashMap();
        if (searchTO.isCaseoneInd()) {
            map.put(1, "信息1");
        }
        if (searchTO.isCasetwoInd()) {
            map.put(2, "信息2");
        }
        if (searchTO.isCasethreeInd()) {
            map.put(3, "信息3");
        }
        if (searchTO.isCasefourInd()) {
            map.put(4, "信息4");
        }
        if (searchTO.isCasefiveInd()) {
            map.put(5, "信息5");
        }
        if (searchTO.isCasesixInd()) {
            map.put(6, "信息6");
        }
        if (searchTO.isCasesevenInd()) {
            map.put(7, "信息7");
        }
        if (searchTO.isCaseeightInd()) {
            map.put(8, "信息8");
        }
        if (searchTO.isCasenineInd()) {
            map.put(9, "信息9");
        }
        if (searchTO.isCasetenInd()) {
            map.put(10, "信息10");
        }

        return map;
    }


    public void setReportsMgr(ReportsMgr reportsMgr) {
        this.reportsMgr = reportsMgr;
    }

    public String getBigPieImgFromByte() {
        if (StringUtil.isEmpty(searchTO.getCaseType())) {
            return null;
        }
        if (!StringUtil.isEmpty(searchTO.getStartDateAsStr())) {

            searchTO.setStartDate(DateUtil.convertParse(searchTO.getStartDateAsStr().substring(8, 10) + "/" + searchTO.getStartDateAsStr().substring(5, 7) + "/" + searchTO.getStartDateAsStr().substring(0, 4)));
        }

        if (!StringUtil.isEmpty(searchTO.getEndDateAsStr())) {

            searchTO.setEndDate(DateUtil.convertParse(searchTO.getEndDateAsStr().substring(8, 10) + "/" + searchTO.getEndDateAsStr().substring(5, 7) + "/" + searchTO.getEndDateAsStr().substring(0, 4)));
        }


        byte[] bigImgByte = getByte("P", searchTO.getCaseType(), searchTO.getReportType(), searchTO.getAgeStr(), searchTO.getInvAmountStr(), searchTO.getStartDate(), searchTO.getEndDate());
        ServletOutputStream out = null;
        try {
            getResponse().setHeader("Content-disposition", "attachment; filename=" + new Date().getTime() + ".png");//改文件名
            getResponse().setContentType("image/jpeg");
            out = getResponse().getOutputStream();
            out.write(bigImgByte);
            out.flush();
        } catch (Exception e) {
            System.out.println("没有图片！！");
        } finally {
            try {
                if (null != out) out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public String getBigColumnImgFromByte() {
        if (StringUtil.isEmpty(searchTO.getCaseType())) {
            return null;
        }
        if (!StringUtil.isEmpty(searchTO.getStartDateAsStr())) {

            searchTO.setStartDate(DateUtil.convertParse(searchTO.getStartDateAsStr().substring(8, 10) + "/" + searchTO.getStartDateAsStr().substring(5, 7) + "/" + searchTO.getStartDateAsStr().substring(0, 4)));
        }

        if (!StringUtil.isEmpty(searchTO.getEndDateAsStr())) {

            searchTO.setEndDate(DateUtil.convertParse(searchTO.getEndDateAsStr().substring(8, 10) + "/" + searchTO.getEndDateAsStr().substring(5, 7) + "/" + searchTO.getEndDateAsStr().substring(0, 4)));
        }

        byte[] bigImgByte = getByte("C", searchTO.getCaseType(), searchTO.getReportType(), searchTO.getAgeStr(), searchTO.getInvAmountStr(), searchTO.getStartDate(), searchTO.getEndDate());
        ServletOutputStream out = null;
        try {
            getResponse().setHeader("Content-disposition", "attachment; filename=" + new Date().getTime() + ".png");//改文件名
            getResponse().setContentType("image/jpeg");
            out = getResponse().getOutputStream();
            out.write(bigImgByte);
            out.flush();
        } catch (Exception e) {
            System.out.println("没有图片！！");
        } finally {
            try {
                if (null != out) out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private byte[] getByte(String type, String caseType, String reportType, String ageStr, String invAmountStr, Date startDate, Date endDate) {
        ArrayList list = new ArrayList();
        if ("01".equals(caseType)) {


            if ("rank".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.RANK_CODE);
            } else if ("industry".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.CASE_WORK_POS_LIST);
            } else if ("hotInd".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.TRUE_CODE);
            } else if ("judgeAccusal".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.CASE_REASON_CODE);
            } else if ("age".equals(reportType) || "invAmount".equals(reportType)) {
                String temp = "";
                String[] s = null;
                if ("age".equals(reportType)) {
                    s = ageStr.split(",");
                }
                if ("invAmount".equals(reportType)) {
                    s = invAmountStr.split(",");
                }
                for (int i = 0; i < s.length; i++) {
                    String value = s[i];
                    if (i == 0) {
                        list.add("0-" + value);
                    } else {
                        list.add(temp + "-" + value);
                    }
                    temp = value;
                }

            }
            if ("C".equals(type)) {

                return reportsMgr.genereateCaseOneColumnChart(reportType, list, startDate, endDate);
            }
            if ("P".equals(type)) {

                return reportsMgr.genereateCaseOnePieChart(reportType, list, startDate, endDate);
            }
        }

        if ("03".equals(caseType)) {

            if ("rank".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.RANK_CODE);
            } else if ("industry".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.CASE_WORK_POS_LIST);
            } else if ("hotInd".equals(reportType)) {
                list = (ArrayList) codeLookupMgr.getCodeListCodes(ReportsMgrImpl.TRUE_CODE);
            } else if ("age".equals(reportType)) {
                String temp = "";
                String[] s = null;
                if ("age".equals(reportType)) {
                    s = ageStr.split(",");
                }

                for (int i = 0; i < s.length; i++) {
                    String value = s[i];
                    if (i == 0) {
                        list.add("0-" + value);
                    } else {
                        list.add(temp + "-" + value);
                    }
                    temp = value;
                }

            }
            if ("C".equals(type)) {

                return reportsMgr.genereateCaseThreeColumnChart(reportType, list, startDate, endDate);
            }
            if ("P".equals(type)) {

                return reportsMgr.genereateCaseThreePieChart(reportType, list, startDate, endDate);
            }

        }
        return null;
    }
}

